home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr37 / satsfaxt.zip / CCPUTILS.ZIP / PARSE.C < prev    next >
Text File  |  1990-01-30  |  5KB  |  114 lines

  1. /*============================================================================*
  2.  *                                                                            *
  3.  *                                   PARSE.C                                  *
  4.  *                                                                            *
  5.  *----------------------------------------------------------------------------*
  6.  *                                                                            *
  7.  *  Function to process command line. The available switches are defined      *
  8.  *  in a table in the application code and passed to this function. For       *
  9.  *  each switch found on the command line, the specified variable (in the     *
  10.  *  switch table) is set to the value on the command line.                    *
  11.  *                                                                            *
  12.  *  CALL    : ParseCommand(int argc, char **argv, STABLE *table,              *
  13.  *                         int tablesize);                                    *
  14.  *  RETURN  : New argc/argv with switches removed (only arguments are left);  *
  15.  *                                                                            *
  16.  *  The following source code is intended to assist developers in             *
  17.  *  creating applications which support the  DCA/Intel Communicating          *
  18.  *  Applications Specification Version 1.0A. It is provided free of charge    *
  19.  *  and on an as-is basis. THE IMPLIED WARRENTIES OF MERCHANTABILITY AND      *
  20.  *  FITNESS FOR A PARTICULAR PURPOSE ARE SPECIFICALLY EXCLUDED. This source   *
  21.  *  code may be modified, enhanced, copied and distributed with applications  *
  22.  *  that support CAS on a royalty free basis.                                 *
  23.  *                                                                            *
  24.  *----------------------------------------------------------------------------*
  25.  *                                                                            *
  26.  *  HISTORY:                                                                  *
  27.  *          - completed 9/14/89.                                              *
  28.  *                                                                            *
  29.  *============================================================================*/
  30.  
  31. #include <stdio.h>
  32. #include <dos.h>
  33. #include "cas.h"
  34. #include "util.h"
  35. #include "parse.h"
  36.  
  37. #define SWITCHCHAR1 '/'
  38. #define SWITCHCHAR2 '-'
  39.  
  40.  
  41. /*---------------------------------------------------------------------------*
  42.  *                                ParseCommand                               *
  43.  *---------------------------------------------------------------------------*
  44.  *  ParseCommand removes all switches from the command line, entering them   *
  45.  *  into the switch table.  argc and argv are adjusted accordingly.          *
  46.  *---------------------------------------------------------------------------*
  47.  *  INPUT :  argc, argv, the calling program's switch table, and the number  *
  48.  *           of elements in the table.                                       *
  49.  *  OUTPUT:  none                                                            *
  50.  *---------------------------------------------------------------------------*/
  51. ParseCommand(int argc, char **argv, STABLE *Table, int NElements)
  52. {
  53.  
  54.     int x;
  55.     int ccArgc;
  56.     char **ccArgv;
  57.     char *Switch;
  58.     STABLE *T;
  59.  
  60.     ccArgc = 1;
  61.     ccArgv = ++argv;
  62.  
  63.     /* Go thru entire command line. */
  64.     while(--argc > 0) {
  65.         /* Check for switch character. */
  66.         if((**argv == SWITCHCHAR1) || (**argv == SWITCHCHAR2)) {
  67.             Switch = (*argv) + 1;
  68.             while(*Switch) {
  69.                 x = NElements;
  70.                 T = Table;
  71.  
  72.                 /* Find the switch in the switch table. */
  73.                 while((x-- >= 0) && (T->Switch != *Switch))
  74.                     T++;
  75.  
  76.                 /* Error if not found, otherwise process it. */
  77.                 if(x < 0)
  78.                    CASError(CASBADARG, TRUE, "", 0);
  79.                 else  {
  80.                     ++Switch;
  81.                     switch(T->Type) {
  82.                         case INT:
  83.                             /* Assign integer variable. */
  84.                             *T->Storage = atoi(Switch);
  85.                             break;
  86.                         case BOOL:
  87.                             /* Assign boolean variable. */
  88.                             *T->Storage = 1;
  89.                             break;
  90.                         case CHAR:
  91.                             /* Assign character variable. */
  92.                             *T->Storage = *Switch++;
  93.                             break;
  94.                         case STRING:
  95.                             /* Assign string variable. */
  96.                             *(char **)T->Storage = Switch++;
  97.                             break;
  98.                     }
  99.                 }
  100.                 Switch = "";
  101.             }
  102.         }
  103.         else {
  104.             /* No switch - then it is an argument (filename etc.). */
  105.             *ccArgv++ = *argv;
  106.             ccArgc++;
  107.         }
  108.         argv++;
  109.     }
  110.     /* Return a new argument count, argv reflects only the arguments - the
  111.        switches have been removed. */
  112.     return ccArgc;
  113. }
  114.